import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:kkhousekeep/models/authorization_community_entity.dart';
import 'package:kkhousekeep/page/authorization/home_page_controller.dart';

import '../../widget/custom_app_bar.dart';

class BuildAuthorizationHomePage extends StatelessWidget {
  const BuildAuthorizationHomePage({
    super.key,
    required this.city,
    required this.community,
  });

  final String city;
  final Community community;

  @override
  Widget build(BuildContext context) {
    return GetBuilder(
        init: AuthorizationHomeController(city, community),
        builder: (controller) => Scaffold(
              appBar: CustomAppBar("请选择你的房屋"),
              body: Obx(() => Column(
                    children: [
                      _buildBreadcrumb(controller),
                      _buildList(controller)
                    ],
                  )),
            ));
  }

  ///面包屑组件
  Widget _buildBreadcrumb(AuthorizationHomeController controller) {
    return Container(
      height: 104.h,
      color: const Color(0xFFF4F5F6),
      alignment: Alignment.centerLeft,
      padding: EdgeInsets.only(left: 32.w),
      child: Wrap(
        children: [
          InkWell(
            child: _customText(community.communityName,
                isBlue: controller.listDataType == ListType.building),
          ),
          Visibility(
              visible: controller.listDataType == ListType.unit ||
                  controller.listDataType == ListType.room,
              child: _customText("/")),
          InkWell(
            onTap: () => controller.changeListTypeAsBuilding(),
            child: _customText(controller.communityBuilding.value,
                isBlue: controller.listDataType == ListType.unit),
          ),
          Visibility(
              visible: controller.listDataType == ListType.room,
              child: _customText("/")),
          InkWell(
              onTap: () => controller.changeListTypeAsUnit(),
              child: _customText(controller.unit.value,
                  isBlue: controller.listDataType == ListType.room))
        ],
      ),
    );
  }

  ///列表组件
  Widget _buildList(AuthorizationHomeController controller) {
    return Expanded(
        child: Container(
      color: const Color(0xFFF4F5F6),
      child: ListView.builder(
          itemCount: controller.listData.length,
          itemBuilder: (context, index) {
            return _buildSingleItem(
                controller.listData.elementAt(index), controller, index);
          }),
    ));
  }

  ///统一的文本组件
  Widget _customText(String content, {bool isBlue = false}) {
    return Text(
      content,
      style: TextStyle(
          color: Color(isBlue ? 0xFF1774E0 : 0xFF000000), fontSize: 32.sp),
    );
  }

  ///单一的列表子项
  Widget _buildSingleItem(
      String content, AuthorizationHomeController controller, int index) {
    return InkWell(
        onTap: () => controller.select(content, index),
        child: Container(
          height: 98.h,
          padding: EdgeInsets.only(left: 32.w, right: 32.w),
          decoration: BoxDecoration(
              color: Colors.white,
              border: Border(
                  bottom:
                      BorderSide(width: 2.h, color: const Color(0xFFF4F5F6)))),
          child: Flex(
            direction: Axis.horizontal,
            children: [
              Expanded(
                  flex: 92,
                  child: Text(
                    content,
                    style: TextStyle(fontSize: 32.sp),
                  )),
              Expanded(
                flex: 8,
                child: Image.asset(
                  "assets/images/authorization_jump_icon.png",
                  width: 48.h,
                  height: 48.h,
                ),
              )
            ],
          ),
        ));
  }
}
